import { defineStore } from 'pinia'
import { getNewRecomment, requestBanners, requestrecommentsonglist, requestsonglistdeail } from "@/api/index"
import { ElMessage } from 'element-plus'
export const homeStore = defineStore('main', {
  state: () => {
    return {
      bannerList: [],
      recommentsonglist: [],
      songlistdetail: {
        tracks: []
      },
      playCurrentIndex: 0,
      playlist: [],
      playsongid: 0,
      singlesong: {},
      playSongIndexInList: '',
      // 首页推荐的新歌
      homeRecommendNewSong: []
    }
  },
  actions: {
    async getbannerlist() {

      let res = await requestBanners()
      if (res.status === 200) {
        this.bannerList = res.data.banners
        return 'ok'
      }
      else {
        ElMessage({
          showClose: true,
          message: '网络错误',
          center: true,
          type: 'error'
        })
        return Promise.reject(new Error('e'))
      }
    },
    async getrecommentsonglist() {
      let res = await requestrecommentsonglist()
      if (res.status === 200) {
        this.recommentsonglist = res.data.result
      } else {
        ElMessage({
          showClose: true,
          message: '网络错误',
          center: true,
          type: 'error'
        })
      }
    },
    // 获取歌单详情
    async getrequestsonglistdetial(id) {

      let res = await requestsonglistdeail(id)
      if (res.status === 200) {
        this.songlistdetail = res.data.playlist
        this.playlist = res.data.playlist.tracks
        console.log('歌单', this.playlist);
      }
      else {
        ElMessage({
          showClose: true,
          message: '网络错误',
          center: true,
          type: 'error'
        })
      }

    },
    // 首页获取新歌
    async asyncGetNewRecomment() {
      let res = await getNewRecomment()
      this.homeRecommendNewSong = res.data.data
      // console.log('我在首页', this.homeRecommendNewSong);
      this.homeRecommendNewSong.length = 10
    },
    // 切换歌
    change(id, item, index) {
      console.log(id, item)
      this.playsongid = id
      this.singlesong = item
      this.playCurrentIndex = index
    }
  }
})